package com.basic.modules.ykf.controller;

import java.util.Arrays;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.ParamUtils;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.sys.controller.AbstractController;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import com.basic.modules.ykf.entity.YkfCustomerSubAccountEntity;
import com.basic.modules.ykf.service.YkfCustomerSubAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfPunishRecordEntity;
import com.basic.modules.ykf.service.YkfPunishRecordService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 处罚管理
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-06-24 11:08:33
 */
@RestController
@RequestMapping("ykf/punishRecord")
public class YkfPunishRecordController extends AbstractController {

    @Autowired
    private YkfPunishRecordService ykfPunishRecordService;

    @Autowired
    private YkfCustomerSubAccountService subAccountService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表-分页
     */
    @PostMapping("/page")
    public R list(@RequestBody ParamsModel<YkfPunishRecordEntity> params){
        PageUtils page = ykfPunishRecordService.queryPage(params);
        return R.ok(page);
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfPunishRecordEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getPunishRecordId())) {
            throw new RRException("参数缺失");
        }
        return R.ok(ykfPunishRecordService.getById(params.getPunishRecordId()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfPunishRecordEntity ykfPunishRecord){
        ValidatorUtils.validateEntity(ykfPunishRecord);
        if (ObjectUtil.isNotEmpty(ykfPunishRecord.getSubAccountId()) && ObjectUtil.isNotEmpty(ykfPunishRecord.getUserId())) {
            YkfCustomerSubAccountEntity subAccount = subAccountService.getById(ykfPunishRecord.getSubAccountId());
            if (ObjectUtil.isNotEmpty(subAccount)) {
                ykfPunishRecord.setAccountNo(subAccount.getAccountNo());
                ykfPunishRecord.setAccountName(subAccount.getAccountName());
            }
            Long userId = ykfPunishRecord.getUserId();
            SysUserEntity user = sysUserService.getById(userId);
            if (ObjectUtil.isNotEmpty(user)) {
                ykfPunishRecord.setNo(user.getNo());
                ykfPunishRecord.setName(user.getName());
                ykfPunishRecord.setFlowerName(user.getFlowerName());
            }
        }
		ykfPunishRecordService.save(ykfPunishRecord);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfPunishRecordEntity ykfPunishRecord){
        ValidatorUtils.validateEntity(ykfPunishRecord);
        if (ObjectUtil.isNotEmpty(ykfPunishRecord.getSubAccountId()) && ObjectUtil.isNotEmpty(ykfPunishRecord.getUserId())) {
            YkfCustomerSubAccountEntity subAccount = subAccountService.getById(ykfPunishRecord.getSubAccountId());
            if (ObjectUtil.isNotEmpty(subAccount)) {
                ykfPunishRecord.setAccountNo(subAccount.getAccountNo());
                ykfPunishRecord.setAccountName(subAccount.getAccountName());
            }
            Long userId = ykfPunishRecord.getUserId();
            SysUserEntity user = sysUserService.getById(userId);
            if (ObjectUtil.isNotEmpty(user)) {
                ykfPunishRecord.setNo(user.getNo());
                ykfPunishRecord.setName(user.getName());
                ykfPunishRecord.setFlowerName(user.getFlowerName());
            }
        }
        SysUserEntity user = getUser();
        ykfPunishRecord.setUpdateTime(DateUtil.date());
        ykfPunishRecord.setUpdateUserId(user.getUserId());
        ykfPunishRecord.setUpdateUserName(user.getUsername());
		ykfPunishRecordService.updateById(ykfPunishRecord);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] punishRecordIds){
		ykfPunishRecordService.removeByIds(Arrays.asList(punishRecordIds));
        return R.ok();
    }

}
